package models.saleman;

import activity.give.GiveWebUserAuth;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.enums.UseStatus;
import models.weixin.WebUser;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 业务员 客户关联表
 */

@Entity
@Table(name = "sale_man_web_users")
public class SaleManWebUser extends BaseModel {


    /**
     * 业务员
     */
    @ManyToOne
    @JoinColumn(name = "sale_man_id")
    public GiveWebUserAuth saleMan;

    /**
     * 客户
     */
    @ManyToOne
    @JoinColumn(name = "web_user_id")
    public WebUser webUser;

    /**
     * 使用状态
     * <p>
     * CURRENT_INTRODUCER // 当前业务员
     * ,PAST_INTRODUCER // 过去的业务员
     */
    @Column(name = "use_status")
    @Enumerated(EnumType.STRING)
    public UseStatus useStatus;

    /**
     * 删除状态
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;


    /**
     * 创建业务员客户关系
     * @param webUser
     * @param giveWebUserAuth
     * @return
     */
    public static SaleManWebUser createSaleManWebUser(WebUser webUser, GiveWebUserAuth giveWebUserAuth) {
        //查询用户当前的业务员 ，如果不存在新建
        SaleManWebUser saleManWebUser = SaleManWebUser.findByUserIdAndStatus(webUser.id);
        if (saleManWebUser == null) {
            if (giveWebUserAuth != null) {
                saleManWebUser = new SaleManWebUser();
                saleManWebUser.saleMan = giveWebUserAuth;
                saleManWebUser.webUser = webUser ;
                saleManWebUser.createAt = new Date();
                saleManWebUser.deleted = DeletedStatus.UN_DELETED ;
                saleManWebUser.useStatus = UseStatus.CURRENT_INTRODUCER ;
                saleManWebUser.save() ;
            }

        }
        return  saleManWebUser ;
    }


    /**
     * 初始化数据使用
     *
     * @param userId
     * @param introduceCode
     * @return
     */
    public static SaleManWebUser findByWebUserIdaAndSaleMan(long userId, String introduceCode) {
        return SaleManWebUser.find("deleted = ?1 and webUser.id = ?2 and saleMan.webUser.introduceCode = ?3", DeletedStatus.UN_DELETED, userId, introduceCode).first();
    }

    /**
     * 查询用户当前业务员
     *
     * @param userId
     * @return
     */
    public static SaleManWebUser findByUserIdAndStatus(long userId) {
        return SaleManWebUser.find("deleted = ?1 and webUser.id = ?2 and useStatus = ?3", DeletedStatus.UN_DELETED, userId, UseStatus.CURRENT_INTRODUCER).first();
    }

    /**
     * 查询客户 最近的一天业务员客户信息
     * @param webUser
     * @return
     */
    public static SaleManWebUser findByWebUser(WebUser webUser){
        return SaleManWebUser.find("deleted = ?1 and webUser = ?2 order by createAt desc" , DeletedStatus.UN_DELETED , webUser).first();
    }

    /**
     * 加载用户所有的业务员
     * @param webUser
     * @return
     */
    public static List<SaleManWebUser> loadByWebUser(WebUser webUser){
        return SaleManWebUser.find("deleted = ?1 and webUser = ?2 order by createAt desc" , DeletedStatus.UN_DELETED , webUser).fetch();
    }

    /**
     * 查询业务员当前所有的客户
     *
     * @param introduceCode
     * @return
     */
    public static List<SaleManWebUser> loadByIntroduceCode(String introduceCode) {
        return SaleManWebUser.find("deleted = ?1 and saleMan.webUser.identityCode = ?2 and useStatus = ?3", DeletedStatus.UN_DELETED, introduceCode, UseStatus.CURRENT_INTRODUCER).fetch();
    }



}
